package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.Product;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface ProductMapper {
    @Select("select a.id,a.name,a.description,a.recommend,a.original_price,a.discount_price,a.pettype_id,b.name as pettypeName,a.stock,a.state,a.img,a.create_time from product a LEFT JOIN pettype b on a.pettype_id = b.id where a.id = #{id};")
    Product selectid(int id);

    @Select("select a.id, a.name, a.description, a.original_price, a.recommend,a.discount_price, a.pettype_id, b.name as pettypeName, a.stock, a.state, a.img, a.create_time " +
            "from product a " +
            "LEFT JOIN pettype b on a.pettype_id = b.id " +
            "where a.pettype_id = #{pettype_id}")
    List<Product> selectpettype_id(String pettype_id);

    @Select("select a.id,a.name,a.description,a.original_price,a.recommend,a.discount_price,a.pettype_id,b.name as pettypeName,a.stock,a.state,a.img,a.create_time" +
            " from product a LEFT JOIN pettype b on a.pettype_id = b.id where a.name like concat('%',#{name},'%');")
    List<Product> selectname(String name);
    @Select("select a.id,a.name,a.description,a.original_price,a.recommend,a.discount_price,a.pettype_id,b.name as pettypeName,a.stock,a.state,a.img,a.create_time" +
            " from product a LEFT JOIN pettype b on a.pettype_id = b.id where a.state= #{state}")
    List<Product> selectstate(String state);
    @Select("select a.id,a.name,a.description,a.original_price,a.recommend,a.discount_price,a.pettype_id,b.name as pettypeName,a.stock,a.state,a.img,a.create_time" +
            " from product a LEFT JOIN pettype b on a.pettype_id = b.id where a.recommend= #{recommend}")
    List<Product> selectrecommend(String recommend);


    @Select("select a.id,a.name,a.description,a.original_price,a.recommend,a.discount_price,a.pettype_id,b.name as pettypeName,a.stock,a.state,a.img,a.create_time,a.update_time from product a LEFT JOIN pettype b on a.pettype_id = b.id;")
    List<Product> findAll();

    @Insert("insert into product(name,description,original_price,discount_price,pettype_id,stock,state,img,create_time,update_time)values(#{name},#{description},#{original_price},#{discount_price},#{pettype_id},#{stock},#{state},#{img},now(),now());")
    int add(Product product);

    @Delete("delete from product where id=#{id};")
    int delete(int id);

    @Update("UPDATE product SET name=#{name},description=#{description},recommend=#{recommend},original_price=#{original_price},discount_price=#{discount_price},pettype_id=#{pettype_id},stock=#{stock},state=#{state},img=#{img},update_time=now() where id= #{id};")
    int update(Product product);
    @Update("UPDATE product SET stock=#{stock},update_time=now() where id= #{id};")
    int updatestock(Product product);
    @Update("UPDATE product SET state=#{state},update_time=now() where id= #{id};")
    int updatestate(Product product);
    // 批量修改status
    @Update({
            "<script>",
            "UPDATE product SET state=#{state}, update_time=now() WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateStatus(@Param("array") int[] ids, @Param("state") String state);
    // 批量修改recommend
    @Update({
            "<script>",
            "UPDATE product SET recommend=#{recommend}, update_time=now() WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateRecommend(@Param("array") int[] ids, @Param("recommend") String recommend);
    // 批量删除
    @Delete({
            "<script>",
            "DELETE FROM product WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int admindelete(@Param("array") int[] ids);
}